using System;
using System.Linq;

namespace LeanUi.ImageBundle
{
    public class NamingHelper 
    {
        private static string NameWithoutExtenstion(string imagename)
        {
            int dotIndex = imagename.LastIndexOf('.');
            int slashIndex = imagename.LastIndexOfAny(new[] { '\\', '/' }) + 1;
            if (dotIndex == -1) {
                dotIndex = imagename.Length;
            }
            return imagename.Substring(slashIndex, dotIndex - slashIndex);
        }

        private static string Capitalize(string s)
        {
            if (string.IsNullOrEmpty(s)) return String.Empty;
            string firstLetter =  s.Substring(0, 1).ToUpper();
            return firstLetter + s.Substring(1);
        }

        private static string PrefixIfStartsWithNumber(string s)
        {
            int i;
            string firstLetter = s.Substring(0, 1);
            return int.TryParse(firstLetter, out i)  ? "Num"+s : s;
        }

        private static string ToCamelCase(string s)
        {
            return string.Join("", s.Split(new[] { '_', '-', '.', ':' })
                                       .Select(x => Capitalize(x))
                                       .ToArray());
        }


        public static string GetNameFor(string filepath)
        {
            return ToCamelCase( PrefixIfStartsWithNumber( NameWithoutExtenstion(filepath) ) );
        }

        //public static string GetInstanceNameFor( string bundle ) {
        //    return ToCamelCase( PrefixIfStartsWithNumber( bundle.ToLower() ) );
        //}

        public static string GetBundleNameFor(string name)
        {
            return ToCamelCase(PrefixIfStartsWithNumber(name.ToLower()));// +"ImageBundle";
        }

        public static string GetBackgroundSpriteClassFor(string imagename)
        {
            return GetNameFor(imagename) + "Bg";
        }
        public static string GetSpriteClassFor(string imagename)
        {
            return GetNameFor(imagename) + "Sprite";
        }
        public static string GetNondisabledSpriteClassFor(string imagename)
        {
            return GetSpriteClassFor(imagename).Replace("DisabledSprite", "Sprite");
        }
    }
}